package com.ecnu.jiagou.damai.controller;

import java.util.List;

import com.ecnu.jiagou.damai.entity.Address;
import com.ecnu.jiagou.damai.entity.Person;
import com.ecnu.jiagou.damai.result.CodeMsg;
import com.ecnu.jiagou.damai.result.Result;
import com.ecnu.jiagou.damai.service.ManageService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.PostMapping;

import javax.swing.text.StyledEditorKit;

//信息管理，包括地址，购票人信息
@RestController
@RequestMapping("/manage")
public class ManageController {
    
    @Autowired
    private ManageService manageService;

    /**
     * 查询地址列表
     * @param userid
     * @return
     */
    @GetMapping("/address")
    public Result<List<Address>> getAddress(Integer userid) {
        return manageService.getAddress(userid);
    }

    /**
     * 添加地址
     * @param address
     * @return
     */
    @PostMapping(value="/createAd")
    public Result<Boolean> createAddress(Address address) {
        return manageService.addAddress(address);
    }
    
    /**
     * 修改地址
     * @param addressid
     * @param phone
     * @param content
     * @return
     */
    @PostMapping(value="/updateAd")
    public Result<Boolean> updateAddress(String addressid,String phone,String content,String name){
        Address address = manageService.getAddressById(Integer.parseInt(addressid));
        if(address == null || phone.length() != 11 || content == null){
            return Result.error(CodeMsg.ERROR);
        }
        address.setPhone(phone);
        address.setContent(content);
        address.setName(name);
        return manageService.updateAddress(address);
    }

    /**
     * 删除地址
     * @param addressid
     * @return
     */
    @PostMapping(value = "/deleteAd")
    public Result<Boolean> deleteAddress(Integer addressid){
        return manageService.deleteAddress(addressid);
    }

    /**
     * 获取常用购票人信息列表
     * @param userid
     * @return
     */
    @GetMapping(value = "/person")
    public Result<List<Person>> getPersons(Integer userid){
        return Result.success(manageService.getPersonList(userid));
    }

    /**
     * 添加常用购票人
     * @param person
     * @return
     */
    @PostMapping(value = "/addPerson")
    public Result<Boolean> addPerson(Person person){
        boolean flag = manageService.addPerson(person);
        if(flag){
            return Result.success(flag);
        }else{
            return Result.error(CodeMsg.ERROR);
        }
    }

    /**
     * 删除常用购票人
     * @param personid
     * @return
     */
    @PostMapping(value = "/deletePerson")
    public Result<Boolean> deletePerson(Integer personid){
        boolean flag = manageService.deletePerson(personid);
        if(flag){
            return Result.success(flag);
        }else{
            return Result.error(CodeMsg.ERROR);
        }
    }

}
